home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / fielddh.exe / DATES.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-03-24  |  24.0 KB  |  682 lines

  1. {$F+,O+}
  2. UNIT Dates;
  3.  
  4.   { Version 1R0 - 1991 03 25                                               }
  5.   {         1R1 - 1991 04 09 - corrected several bugs, and                 }
  6.   {                          - deleted <JulianDa2>, <Da2OfWeek> and        }
  7.   {                            <JulianDa2ToDate> - all found to be not     }
  8.   {                            completely reliable.                        }
  9.  
  10. INTERFACE
  11.  
  12.   { These routines all assume that the year (y, y1) value is supplied in a }
  13.   { form that includes the century (i.e., in YYYY form).  No checking is   }
  14.   { performed to ensure that a month (m, m1) value is in the range 1..12   }
  15.   { or that a day (d, d1) value is in the range 1..28,29,30,31.  The       }
  16.   { FUNCTION ValidDate may be used to check for valid month and day        }
  17.   { parameters. FUNCTION DayOfYearToDate returns month and day (m, d) both }
  18.   { = 0 if the day-of-the-year (nd) is > 366 for a leap-year or > 365 for  }
  19.   { other years.                                                           }
  20.  
  21.   { NOTE: As written, FUNCTION Secs100 requires the presence of a 80x87    }
  22.   { co-processor.  Its declaration and implementation may be altered to    }
  23.   { REAL to make use of the floating-point emulation.                      }
  24.  
  25.   { Because the Gregorian calendar was not implemented in all countries at }
  26.   { the same time, these routines are not guaranteed to be valid for all   }
  27.   { dates. The real utility of these routines is that they will not fail   }
  28.   { on December 31, 1999 - as will many algorithms used in MIS programs    }
  29.   { implemented on mainframes.                                             }   
  30.  
  31.   { The routines are NOT highly optimized - I have tried to maintain the   }
  32.   { style of the algorithms presented in the sources I indicate. Any       }
  33.   { suggestions for algorithmic or code improvements will be gratefully    }
  34.   { accepted.  This implementation is in the public domain - no copyright  }
  35.   { is claimed.  No warranty either express or implied is given as to the  }
  36.   { correctness of the algorithms or their implementation.                 }
  37.  
  38.   { Author: Charles B. Chapman, London, Ontario, Canada [74370,516]        }
  39.   { Thanks to Leonard Erickson who supplied a test suite of values.        }
  40.  
  41.   FUNCTION IsLeap (y : WORD) : BOOLEAN;
  42.  
  43.   FUNCTION ValidDate (y, m, d : WORD) : BOOLEAN;
  44.   FUNCTION ValidDate_Str (Str         : string;                     {DWH}
  45.                           VAR Y, M, D : word;
  46.                           VAR Err_Str : string) : boolean;
  47.   FUNCTION ValidTime_Str (Str         : string;                     {DWH}
  48.                           VAR H, M, S : word;
  49.                           VAR Err_Str : string) : boolean;
  50.  
  51.   FUNCTION DayOfYear (y, m, d : WORD) : WORD;
  52.   FUNCTION JulianDay (y, m, d : WORD) : LONGINT;
  53.   FUNCTION JJ_JulianDay (y, m, d : word) : LONGINT;                 {DWH}
  54.  
  55.   FUNCTION DayOfWeek (y, m, d : WORD) : WORD;
  56.   FUNCTION DayOfWeek_Str (y, m, d : WORD) : String;                 {DWH}
  57.  
  58.   FUNCTION TimeStr   (h, m, s, c : WORD) : STRING;
  59.   FUNCTION TimeStr2  (h, m, s : WORD) : STRING;
  60.   FUNCTION SIDateStr (y, m, d : WORD; SLen : BYTE; FillCh : CHAR) : STRING;
  61.   FUNCTION MDYR_Str  (y, m, d : WORD): STRING;                      {DWH}
  62.  
  63.   FUNCTION Secs100 (h, m, s, c : WORD) : DOUBLE;
  64.   PROCEDURE DayOfYearToDate (nd, y : WORD; VAR m, d : WORD);
  65.  
  66.   PROCEDURE JulianDayToDate (nd : LONGINT; VAR y, m, d : WORD);
  67.   PROCEDURE JJ_JulianDayToDate (nd : LONGINT; VAR y, m, d : WORD);  {DWH}
  68.  
  69.   PROCEDURE DateOfEaster (Yr : WORD; VAR Mo, Da : WORD);
  70.   PROCEDURE AddDays (y, m, d : WORD; plus : LONGINT; VAR y1, m1, d1 : WORD);
  71.  
  72.   FUNCTION Lotus_Date_Str (nd : LONGINT) : string;                  {DWH}
  73. {==========================================================================}
  74.  
  75. IMPLEMENTATION
  76.   USES
  77.     Dos;
  78.  
  79. {==========================================================================}
  80.  
  81.   FUNCTION IsLeap (y : WORD) : BOOLEAN;
  82.  
  83.   { Returns TRUE if <y> is a leap-year                                     }
  84.  
  85.   BEGIN
  86.     IF y MOD 4 <> 0 THEN
  87.       IsLeap := FALSE
  88.     ELSE
  89.       IF y MOD 100 = 0 THEN
  90.         IF y MOD 400 = 0 THEN
  91.           IsLeap := TRUE
  92.         ELSE
  93.           IsLeap := FALSE
  94.       ELSE
  95.         IsLeap := TRUE
  96.   END;  { IsLeap }
  97.  
  98. {==========================================================================}
  99.  
  100.   FUNCTION DayOfYear (y, m, d : WORD) : WORD;
  101.  
  102.   { function IDAY from remark on CACM Algorithm 398                        }
  103.   { Computes day of the year for a given calendar date                     }
  104.   { GIVEN:   y - year                                                      }
  105.   {          m - month                                                     }
  106.   {          d - day                                                       }
  107.   { RETURNS: day-of-the-year (1..366, given valid input)                   }
  108.  
  109.   VAR
  110.     yy, mm, dd, Tmp1 : LONGINT;
  111.   BEGIN
  112.     yy := y;
  113.     mm := m;
  114.     dd := d;
  115.     Tmp1 := (mm + 10) DIV 13;
  116.     DayOfYear :=  3055 * (mm + 2) DIV 100 - Tmp1 * 2 - 91 +
  117.                   (1 - (yy - yy DIV 4 * 4 + 3) DIV 4 +
  118.                   (yy - yy DIV 100 * 100 + 99) DIV 100 -
  119.                   (yy - yy DIV 400 * 400 + 399) DIV 400) * Tmp1 + dd
  120.   END;  { DayOfYear }
  121.  
  122. {==========================================================================}
  123.  
  124.   FUNCTION JulianDay (y, m, d : WORD) : LONGINT;
  125.  
  126.   { procedure JDAY from CACM Alorithm 199                                  }
  127.   { Computes Julian day number for any Gregorian Calendar date             }
  128.   { GIVEN:   y - year                                                      }
  129.   {          m - month                                                     }
  130.   {          d - day                                                       }
  131.   { RETURNS: Julian day number (astronomically, for the day                }
  132.   {          beginning at noon) on the given date.                         }
  133.  
  134.   VAR
  135.     Tmp1, Tmp2, Tmp3, Tmp4, Tmp5 : LONGINT;
  136.   BEGIN
  137.     IF m > 2 THEN
  138.       BEGIN
  139.         Tmp1 := m - 3;
  140.         Tmp2 := y
  141.       END
  142.     ELSE
  143.       BEGIN
  144.         Tmp1 := m + 9;
  145.         Tmp2 := y - 1
  146.       END;
  147.     Tmp3 := Tmp2 DIV 100;
  148.     Tmp4 := Tmp2 MOD 100;
  149.     Tmp5 := d;
  150.     JulianDay := (146097 * Tmp3) DIV 4 + (1461 * Tmp4) DIV 4 +
  151.                  (153 * Tmp1 + 2) DIV 5 + Tmp5 + 1721119
  152.   END;  { JulianDay }
  153.  
  154. {==========================================================================}
  155.   
  156.   PROCEDURE DayOfYearToDate (nd, y : WORD; VAR m, d : WORD);
  157.                                                          
  158.   { procedure CALENDAR from CACM Algorithm 398                             }
  159.   { Computes month and day from given year and day of the year             }
  160.   { GIVEN:   nd - day-of-the-year (1..366)                                 }
  161.   {          y - year                                                      }
  162.   { RETURNS: m - month                                                     }
  163.   {          d - day                                                       }
  164.  
  165.   VAR
  166.     Tmp1, Tmp2, Tmp3, Tmp4, DaYr : LONGINT; 
  167.   BEGIN
  168.     DaYr := nd;
  169.     IF (DaYr = 366) AND (DayOfYear (y, 12, 31) <> 366) THEN
  170.       DaYr := 999;
  171.     IF DaYr <= 366 THEN
  172.       BEGIN
  173.         IF y MOD 4 = 0 THEN
  174.           Tmp1 := 1
  175.         ELSE
  176.           Tmp1 := 0;
  177.         IF (y MOD 400 = 0) OR (y MOD 100 <> 0) THEN
  178.           Tmp2 := Tmp1
  179.         ELSE
  180.           Tmp2 := 0;
  181.         Tmp1 := 0;
  182.         IF DaYr > Tmp2 + 59 THEN
  183.           Tmp1 := 2 - Tmp2;
  184.         Tmp3 := DaYr + Tmp1;
  185.         Tmp4 := ((Tmp3 + 91) * 100) DIV 3055;
  186.         d := ((Tmp3 + 91) - (Tmp4 * 3055) DIV 100);
  187.         m := (Tmp4 - 2)
  188.       END
  189.     ELSE
  190.       BEGIN
  191.         d := 0;
  192.         m := 0
  193.       END
  194.   END;  { DayOfYearToDate }
  195.  
  196. {==========================================================================}
  197.  
  198.   PROCEDURE JulianDayToDate (nd : LONGINT; VAR y, m, d : WORD);
  199.  
  200.   { procedure JDATE from CACM Algorithm 199                                }
  201.   { Computes calendar date from a given Julian day number for any          }
  202.   { valid Gregorian calendar date                                          }
  203.   { GIVEN:   nd - Julian day number (2440000 --> 1968 5 23)                }
  204.   { RETURNS: y - year                                                      }
  205.   {          m - month                                                     }
  206.   {          d - day                                                       }
  207.  
  208.   VAR
  209.     Tmp1, Tmp2, Tmp3 : LONGINT;
  210.   BEGIN
  211.     Tmp1 := nd - 1721119;
  212.     Tmp3 := (4 * Tmp1 - 1) DIV 146097;
  213.     Tmp1 := (4 * Tmp1 - 1) MOD 146097;
  214.     Tmp2 := Tmp1 DIV 4;
  215.     Tmp1 := (4 * Tmp2 + 3) DIV 1461;
  216.     Tmp2 := (4 * Tmp2 + 3) MOD 1461;
  217.     Tmp2 := (Tmp2 + 4) DIV 4;
  218.     m := ((5 * Tmp2 - 3) DIV 153);
  219.     Tmp2 := (5 * Tmp2 - 3) MOD 153;
  220.     d := ((Tmp2 + 5) DIV 5);
  221.     y := (100 * Tmp3 + Tmp1);
  222.     IF m < 10 THEN
  223.       m := m + 3
  224.     ELSE
  225.       BEGIN
  226.         m := m - 9;
  227.         y := y + 1
  228.       END
  229.   END;  { JulianDayToDate }
  230.  
  231. {==========================================================================}
  232.  
  233.   PROCEDURE DateOfEaster (Yr : WORD; VAR Mo, Da : WORD);
  234.  
  235.   { Algorithm "E" from Knuth's "Art of Computer Programming", vol. 1       }
  236.   { Computes date of Easter for any year in the Gregorian calendar         }
  237.   { The local variables are the variable names used by Knuth.              }
  238.   { GIVEN:   Yr - year                                                     }
  239.   { RETURNS: Mo - month of Easter (3 or 4)                                 }
  240.   {          Da - day of Easter                                            }
  241.  
  242.   VAR
  243.     G, C, X, Z, D, E, N : LONGINT;
  244.   BEGIN
  245.   { Golden number of the year in Metonic cycle   }
  246.     G := Yr MOD 19 + 1;
  247.   { Century  }
  248.     C := Yr DIV 100 + 1;
  249.   { Corrections: }
  250.   { <X> is the no. of years in which leap-year was dropped in }
  251.   { order to keep step with the sun   }
  252.   { <Z> is a special correction to synchronize Easter with the }
  253.   { moon's orbit  . }
  254.     X := (3 * C) DIV 4 - 12;
  255.     Z := (8 * C + 5) DIV 25 - 5;
  256.   { <D> Find Sunday   }
  257.     D := (5 * Yr) DIV 4 - X - 10;
  258.   { Set Epact  }
  259.     E := (11 * G + 20 + Z - X) MOD 30;
  260.     IF E < 0 THEN
  261.       E := E + 30;
  262.     IF ((E = 25) AND (G > 11)) OR (E = 24) THEN
  263.       E := E + 1;
  264.   { Find full moon - the Nth of MARCH is a "calendar" full moon }
  265.     N := 44 - E;
  266.     IF N < 21 THEN
  267.       N := N + 30;
  268.   { Advance to Sunday }
  269.     N := N + 7 - ((D + N) MOD 7);
  270.   { Get Month and Day }
  271.     IF N > 31 THEN
  272.       BEGIN
  273.         Mo := 4;
  274.         Da := N - 31
  275.       END
  276.     ELSE
  277.       BEGIN
  278.         Mo := 3;
  279.         Da := N
  280.       END
  281.   END; { DateOfEaster }
  282.  
  283. {==========================================================================}
  284.  
  285.   FUNCTION SIDateStr (y, m, d : WORD; SLen : BYTE; FillCh : CHAR) : STRING;
  286.  
  287.   { Returns date <y>, <m>, <d> converted to a string in SI format.  If     }
  288.   { <Slen> = 10, the string is in form YYYY_MM_DD; If <SLen> = 8, in form  }
  289.   { YY_MM_DD; otherwise a NULL string is returned.  The character between  }
  290.   { values is <FillCh>.                                                    }
  291.   { For correct Systeme-Internationale date format, the call should be:    }
  292.   {   SIDateStr (Year, Month, Day, 10, ' ');                               }
  293.   { IF <y>, <m> & <d> are all = 0, Runtime library PROCEDURE GetDate is    }
  294.   { called to obtain the current date.                                     }
  295.  
  296.   VAR
  297.     s2 : STRING[2];
  298.     s4 : STRING[4];
  299.     DStr : STRING[10];
  300.     Index : BYTE;
  301.     dw : WORD;
  302.   BEGIN
  303.     IF (SLen <> 10) AND (SLen <> 8) THEN
  304.       DStr[0] := Chr (0)
  305.     ELSE
  306.       BEGIN
  307.         IF (y = 0) AND (m = 0) AND (d = 0) THEN
  308.           GetDate (y, m, d, dw);
  309.         IF SLen = 10 THEN
  310.           BEGIN
  311.             Str (y:4, s4);
  312.             DStr[1] := s4[1];
  313.             DStr[2] := s4[2];
  314.             DStr[3] := s4[3];
  315.             DStr[4] := s4[4];
  316.             Index := 5
  317.           END
  318.         ELSE
  319.           IF SLen = 8 THEN
  320.             BEGIN
  321.               Str (y MOD 100:2, s2);
  322.               DStr[1] := s2[1];
  323.               DStr[2] := s2[2];
  324.               Index := 3
  325.             END;
  326.         DStr[Index] := FillCh;
  327.         Inc (Index);
  328.         Str (m:2, s2);
  329.         IF s2[1] = ' ' THEN
  330.           DStr[Index] := '0'
  331.         ELSE
  332.           DStr[Index] := s2[1];
  333.         DStr[Index+1] := s2[2];
  334.         Index := Index + 2;
  335.         DStr[Index] := FillCh;
  336.         Inc (Index);
  337.         Str (d:2, s2);
  338.         IF s2[1] = ' ' THEN
  339.           DStr[Index] := '0'
  340.         ELSE
  341.           DStr[Index] := s2[1];
  342.         DStr[Index+1] := s2[2];
  343.         DStr[0] := Chr (SLen)
  344.       END;
  345.     SIDateStr := DStr
  346.   END;  { SIDateStr }
  347.  
  348. {==========================================================================}
  349.  
  350.   FUNCTION TimeStr (h, m, s, c : WORD) : STRING;
  351.  
  352.   { Returns the time <h>, <m>, <s> and <c> formatted in a string:          }
  353.   { "HH:MM:SS.CC"                                                          }
  354.   { This function does NOT check for valid string length.                  }
  355.   {                                                                        }
  356.   { IF <h>, <m>, <s> & <c> all = 0, the Runtime PROCEDURE GetTime is       }
  357.   { called to get the current time.                                        }
  358.  
  359.   VAR
  360.     sh, sm, ss, sc : STRING[2];
  361.   BEGIN
  362.     IF h + m + s + c = 0 THEN
  363.       GetTime (h, m, s, c);
  364.     Str (h:2, sh);
  365.     IF sh[1] = ' ' THEN
  366.       sh[1] := '0';
  367.     Str (m:2, sm);
  368.     IF sm[1] = ' ' THEN
  369.       sm[1] := '0';
  370.     Str (s:2, ss);
  371.     IF ss[1] = ' ' THEN
  372.       ss[1] := '0';
  373.     Str (c:2, sc);
  374.     IF sc[1] = ' ' THEN
  375.       sc[1] := '0';
  376.     TimeStr := Concat (sh, ':', sm, ':', ss, '.', sc)
  377.   END;  { TimeStr }
  378.  
  379.   FUNCTION TimeStr2 (h, m, s : WORD) : STRING;
  380.  
  381.   { Returns the time <h>, <m>, and <s>  formatted in a string:             }
  382.   { "HH:MM:SS"                                                             }
  383.   { This function does NOT check for valid string length.                  }
  384.   {                                                                        }
  385.   { IF <h>, <m>, & <c> all = 0, the Runtime PROCEDURE GetTime is           }
  386.   { called to get the current time.                                        }
  387.  
  388.   VAR
  389.     c              : word;
  390.     sh, sm, ss, sc : STRING[2];
  391.   BEGIN
  392.     IF h + m + s = 0 THEN
  393.       GetTime (h, m, s, c);
  394.     Str (h:2, sh);
  395.     IF sh[1] = ' ' THEN
  396.       sh[1] := '0';
  397.     Str (m:2, sm);
  398.     IF sm[1] = ' ' THEN
  399.       sm[1] := '0';
  400.     Str (s:2, ss);
  401.     IF ss[1] = ' ' THEN
  402.       ss[1] := '0';
  403.     TimeStr2 := Concat (sh, ':', sm, ':', ss)
  404.   END;  { TimeStr2 }
  405.  
  406. {==========================================================================}
  407.   FUNCTION MDYR_Str (y, m, d : WORD): STRING;     {dwh}
  408.  
  409.   { Returns the date <y>, <m>, <d> formatted in a string:                  }
  410.   { "MM/DD/YYYY"                                                           }
  411.   { This function does NOT check for valid string length.                  }
  412.   {                                                                        }
  413.   { IF <m>, <d>, & <y> all = 0, the Runtime PROCEDURE GetDate is           }
  414.   { called to get the current date.                                        }
  415.  
  416.   VAR
  417.     sm, sd     : STRING[2];
  418.     sy         : STRING[4];
  419.     dont_care  : word;
  420.   BEGIN
  421.     IF y + m + d = 0 THEN
  422.       GetDate (y, m, d, dont_care);
  423.     Str (m:2, sm);
  424.     IF sm[1] = ' ' THEN
  425.       sm[1] := '0';
  426.     Str (d:2, sd);
  427.     IF sd[1] = ' ' THEN
  428.       sd[1] := '0';
  429.     Str (y:4, sy);
  430.     MDYR_Str := Concat (sm, '/', sd, '/', sy)
  431.   END;  { MDYR_Str }
  432.  
  433.  
  434. {==========================================================================}
  435.  
  436.   FUNCTION Secs100 (h, m, s, c : WORD) : DOUBLE;
  437.  
  438.   { Returns the given time <h>, <m>, <s> and <c> as a floating-point       }
  439.   { value in seconds (presumably valid to .01 of a second).                }
  440.   {                                                                        }
  441.   { IF <h>, <m>, <s> & <c> all = 0, the Runtime PROCEDURE GetTime is       }
  442.   { called to get the current time.                                        }
  443.  
  444.   BEGIN
  445.     IF h + m + s + c = 0 THEN
  446.       GetTime (h, m, s, c);
  447.     Secs100 :=  (h * 60.0 + m) * 60.0 + s + (c * 0.01)
  448.   END;  { Secs100 }
  449.  
  450. {==========================================================================}
  451.  
  452.   PROCEDURE AddDays (y, m, d : WORD; plus : LONGINT; VAR y1, m1, d1 : WORD);
  453.  
  454.   { Computes the date <y1>, <m1>, <d1> resulting from the addition of      }
  455.   { <plus> days to the calendar date <y>, <m>, <d>.                        }
  456.  
  457.   VAR
  458.     JulDay : LONGINT;
  459.   BEGIN
  460.     JulDay := JulianDay (y, m, d) + plus;
  461.     JulianDayToDate (JulDay, y1, m1, d1)
  462.   END;  { AddDays }
  463.  
  464. {==========================================================================}
  465.  
  466.   FUNCTION ValidDate (y, m, d : WORD) : BOOLEAN;
  467.  
  468.   { Returns TRUE if the date <y> <m> <d> is valid.                         }
  469.  
  470.   VAR
  471.     JulDay : LONGINT;
  472.     ycal, mcal, dcal : WORD;
  473.   BEGIN
  474.     JulDay := JulianDay (y, m, d);
  475.     JulianDayToDate (JulDay, ycal, mcal, dcal);
  476.     ValidDate := (y = ycal) AND (m = mcal) AND (d = dcal)
  477.   END;  { ValidDate }
  478.  
  479. {==========================================================================}
  480.  
  481.   FUNCTION DayOfWeek (y, m, d : WORD) : WORD;
  482.  
  483.   { Returns the Day-of-the-week (0 = Sunday) (Zeller's congruence) from an }
  484.   { algorithm IZLR given in a remark on CACM Algorithm 398.                }
  485.  
  486.   VAR
  487.     Tmp1, Tmp2, yy, mm, dd : LONGINT;
  488.   BEGIN
  489.     yy := y;
  490.     mm := m;
  491.     dd := d;
  492.     Tmp1 := mm + 10;
  493.     Tmp2 := yy + (mm - 14) DIV 12;
  494.     DayOfWeek :=  ((13 *  (Tmp1 - Tmp1 DIV 13 * 12) - 1) DIV 5 +
  495.                   dd + 77 + 5 * (Tmp2 - Tmp2 DIV 100 * 100) DIV 4 +
  496.                   Tmp2 DIV 400 - Tmp2 DIV 100 * 2) MOD 7;
  497.   END;  { DayOfWeek }
  498.  
  499. {==========================================================================}
  500. FUNCTION DayOfWeek_Str (y, m, d : WORD) : String;
  501. begin
  502.   CASE DayOfWeek (y, m, d) of
  503.    0: DayOfWeek_Str := 'SUNDAY';
  504.    1: DayOfWeek_Str := 'MONDAY';
  505.    2: DayOfWeek_Str := 'TUESDAY';
  506.    3: DayOfWeek_Str := 'WEDNESDAY';
  507.    4: DayOfWeek_Str := 'THURSDAY';
  508.    5: DayOfWeek_Str := 'FRIDAY';
  509.    6: DayOfWeek_Str := 'SATURDAY';
  510.   end; {case}
  511. end; {dayofweek_str}
  512.  
  513.  
  514. {==========================================================================}
  515. FUNCTION JJ_JulianDay (y, m, d : word) : LONGINT;
  516.   {*  format     5 position = last 2 digits of year+DayOfYear *}
  517. begin
  518.   JJ_JulianDay:= ((LongInt(y) Mod 100)*1000+ DayOfYear(y,m,d));
  519. end; {jj_julianday}
  520.  
  521.  
  522. {==========================================================================}
  523. PROCEDURE JJ_JulianDayToDate (nd : LONGINT; VAR y, m, d : WORD);
  524.   {*  format     nd=5 positions   last 2 digits of year+DayOfYear *}
  525. VAR
  526.   Manu_Day : word;
  527. BEGIN
  528.   y := (nd DIV 1000); {year}
  529.                     {dayofyear}
  530.   DayOfYearToDate ( (nd MOD 1000), y, m, d);
  531. END;  { JulianDayToDate }
  532.  
  533. {==========================================================================}
  534. FUNCTION Lotus_Date_Str (nd : LONGINT) : string;
  535.    {* lotus is strange the ND is the number of days SINCE 12/31/1899 *}
  536.    {*         which is the JULIAN day 2415020                        *}
  537.    {*   Return format is MM/DD/YYYY                                  *}
  538. var
  539.   y,m,d : word;
  540. begin
  541.   JulianDayToDate (nd+2415020-1, y,m,d);
  542.   Lotus_Date_Str := MDYr_Str (y,m,d);
  543. end; {lotus_date_str}
  544.  
  545.  
  546.  
  547. {==========================================================================}
  548. FUNCTION ValidDate_Str (Str         : string;
  549.                         VAR Y, M, D : word;
  550.                         VAR Err_Str : string) : boolean;
  551.    {* returns TRUE when Str is valid  MM/DD/YYYY  or MM-DD-YYYY      *}
  552.    {*         the values are ranged checked and the date is also     *}
  553.    {*         checked for existance                                  *}
  554.    {*         Y, M, D are filled in with the values.                 *}
  555. var
  556.   Err_Code               : integer;
  557.   Long_Int               : LongInt;
  558.   Slash1, Slash2         : byte;
  559. begin
  560.   Err_Str  := '';
  561.   Err_Code := 0;
  562.  
  563.   IF (Length (Str) < 8)
  564.     THEN Err_Str := 'Date must be   12/31/1999  format'
  565.   ELSE
  566.     BEGIN
  567.       Slash1 := POS ('/', Str);
  568.       IF (Slash1 > 0)
  569.         THEN Slash2 := POS ('/', COPY (Str, Slash1+1, LENGTH(Str))) + Slash1
  570.       ELSE
  571.         BEGIN
  572.           Slash2 := 0;
  573.           Slash1 := POS ('-', Str);
  574.           IF (Slash1 > 0)
  575.             THEN Slash2 := POS ('-', COPY (Str, Slash1+1,
  576.                                              LENGTH(Str))) + Slash1;
  577.         END;
  578.  
  579.       IF ((Slash1 =  Slash2) or (Slash2 = 0))
  580.         THEN Err_Str := 'Date String must have either "-" or "/"'+
  581.                         ' such as (12/01/1999)'
  582.       ELSE
  583.         BEGIN
  584.           VAL (COPY(Str, 1,(Slash1-1)), Long_Int, Err_Code);
  585.           IF ((Err_Code <> 0) or (Long_Int < 1) or (Long_Int > 12))
  586.             THEN Err_Str := 'Month must be a number 1..12!'
  587.  
  588.           ELSE
  589.             BEGIN
  590.               M := Long_Int;
  591.               VAL (COPY(Str, (Slash1+1),(Slash2-Slash1-1)),
  592.                            Long_Int, Err_Code);
  593.               IF ((Err_Code <> 0) or (Long_Int < 1) or (Long_Int > 31))
  594.                 THEN Err_Str := 'Day must be a number 1..31!'
  595.  
  596.               ELSE
  597.                 BEGIN
  598.                   D := Long_Int;
  599.                   VAL (COPY(Str, (Slash2+1),LENGTH(Str)), Long_Int, Err_Code);
  600.                   IF ((Err_Code <> 0) or (Long_Int < 1900))
  601.                     THEN Err_Str := 'Year must be a number greater than 1900!'
  602.                     ELSE Y := Long_Int;
  603.                 END;
  604.             END;
  605.         END;
  606.     END; {if long enough}
  607.  
  608.   IF ((LENGTH(Err_Str) = 0) and (NOT DATES.ValidDate (Y, M, D)))
  609.     THEN Err_Str := 'ERR: Date does not exist!!!!';
  610.  
  611.   IF (LENGTH(Err_Str) = 0)
  612.     THEN ValidDate_Str := TRUE
  613.     ELSE ValidDate_Str := FALSE;
  614.  
  615. END; {validdate_str}
  616.  
  617. {==========================================================================}
  618. FUNCTION ValidTime_Str (Str         : string;
  619.                         VAR H, M, S : word;
  620.                         VAR Err_Str : string) : boolean;
  621.    {* returns TRUE when Str is valid  HH:MM  or HH:MM:SS             *}
  622.    {*         also H, M, S are filled in with the values.            *}
  623. var
  624.   Err_Code               : integer;
  625.   Long_Int               : LongInt;{use longint with VAL to prevent overflow}
  626.   Sep1, Sep2             : byte;
  627.   Count                  : byte;
  628. begin
  629.   Err_Str  := '';
  630.   Err_Code := 0;
  631.  
  632.   IF (Length (Str) < 4)
  633.     THEN Err_Str := 'Time must be   HH:MM or HH:MM:SS  format'
  634.   ELSE
  635.     BEGIN
  636.       Sep1 := POS (':', Str);
  637.       IF (Sep1 = 0)
  638.         THEN Err_Str := 'Time String must have either ":" '+
  639.                         ' such as  HH:MM  or  HH:MM:SS'
  640.  
  641.       ELSE
  642.         BEGIN
  643.           VAL (COPY(Str, 1,(Sep1-1)), Long_Int, Err_Code);
  644.           IF ((Err_Code <> 0) or (Long_Int < 1) or (Long_Int > 24))
  645.             THEN Err_Str := 'Hour must be a number 1..24!'
  646.  
  647.           ELSE
  648.             BEGIN
  649.               H := Long_Int;
  650.               Sep2 := POS (':', COPY (Str, Sep1+1, LENGTH(Str))) + Sep1;
  651.               IF (Sep2 = Sep1)
  652.                 THEN Count := LENGTH(Str)
  653.                 ELSE Count := Sep2-Sep1-1;
  654.               VAL (COPY(Str,(Sep1+1),Count), Long_Int, Err_Code);
  655.               IF ((Err_Code <> 0) or (Long_Int < 0) or (Long_Int > 59))
  656.                 THEN Err_Str := 'Minute must be a number 0..59!'
  657.  
  658.               ELSE
  659.                 BEGIN
  660.                   M := Long_Int;
  661.                   IF (Sep2 <> Sep1) THEN
  662.                     BEGIN
  663.                       VAL (COPY(Str, (Sep2+1),LENGTH(Str)), Long_Int, Err_Code);
  664.                       IF ((Err_Code <> 0) or (Long_Int < 0) or (Long_Int > 59))
  665.                         THEN Err_Str := 'Second must be a number 0..59!'
  666.                         ELSE S := Long_Int;
  667.                     END
  668.                   ELSE S := 0;
  669.                 END;
  670.             END;
  671.         END;
  672.     END; {if long enough}
  673.  
  674.   IF (LENGTH(Err_Str) = 0)
  675.     THEN ValidTime_Str := TRUE
  676.     ELSE ValidTime_Str := FALSE;
  677.  
  678. END; {validtime_str}
  679.  
  680. END. {unit dates}
  681.  
  682.